package com.hadluo.qq.server.mapper;

import com.hadluo.qq.server.entity.GroupMember;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 群组成员Mapper接口
 */
@Mapper
public interface GroupMemberMapper {

    @Insert("INSERT INTO group_members (group_id, user_id, role, alias, join_time) " +
            "VALUES (#{groupId}, #{userId}, #{role}, #{alias}, #{joinTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(GroupMember member);

    @Select("SELECT * FROM group_members WHERE group_id = #{groupId}")
    List<GroupMember> findByGroupId(Long groupId);

    @Select("SELECT * FROM group_members WHERE group_id = #{groupId} AND user_id = #{userId}")
    GroupMember findByGroupIdAndUserId(Long groupId, Long userId);

    @Select("SELECT COUNT(*) > 0 FROM group_members WHERE group_id = #{groupId} AND user_id = #{userId}")
    boolean existsByGroupIdAndUserId(Long groupId, Long userId);

    @Delete("DELETE FROM group_members WHERE id = #{id}")
    void delete(GroupMember member);

    @Delete("DELETE FROM group_members WHERE group_id = #{groupId}")
    void deleteByGroupId(Long groupId);
}